<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/7
 * Time: 11:50
 */

namespace app\modules\api\models;

use yii;
use yii\db\Query;

class Quote
{
    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'quote2';
    }

    /**大客户组分页信息
     * @param $p
     * @param $s
     * @param array $where
     * @return array
     */
    public function list($p, $s, $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['service'])) {
            $db->andWhere(['service'=>$where['service']]);
        }
        if(!empty($where['product_num'])) {
            $db->andWhere(['product_num'=>$where['product_num']]);
        }
        if(!empty($where['leader'])) {
            $db->andWhere(['leader'=>$where['leader']]);
        }
        if(!empty($where['product_status'])) {
            $db->andWhere(['product_status'=>$where['product_status']]);
        }
        if(!empty($where['product_num'])) {
            $db->andWhere(['product_num'=>$where['product_num']]);
        }
        if(!empty($where['begin_time'])) {
            $db->andWhere(['>=','update_at',$where['begin_time']]);
        }
        if(!empty($where['end_time'])) {
            $db->andWhere(['<=','update_at',$where['endtime']]);
        }
        if(!empty($where['product_name'])) {
            $db->andWhere(['like','product_name',$where['product_name']]);
        }
        if(!empty($where['product_name_en'])) {
            $db->andWhere(['like','product_name_en',$where['product_name_en']]);
        }
        $list = $db->orderBy(['create_at' => SORT_DESC])->offset(($p - 1) * $s)->limit($s)->all();
        return $list;
    }

    /**大客户组报价size
     * @param array $where
     * @return int
     */
    public function size($where = [])
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if (!empty($where['service'])) {
            $db->andWhere(['service'=>$where['service']]);
        }
        if(!empty($where['product_num'])) {
            $db->andWhere(['product_num'=>$where['product_num']]);
        }
        if(!empty($where['leader'])) {
            $db->andWhere(['leader'=>$where['leader']]);
        }
        if(!empty($where['product_status'])) {
            $db->andWhere(['product_status'=>$where['product_status']]);
        }
        if(!empty($where['product_num'])) {
            $db->andWhere(['product_num'=>$where['product_num']]);
        }
        if(!empty($where['begin_time'])) {
            $db->andWhere(['>=','update_at',$where['begin_time']]);
        }
        if(!empty($where['end_time'])) {
            $db->andWhere(['<=','update_at',$where['endtime']]);
        }
        if(!empty($where['product_name'])) {
            $db->andWhere(['like','product_name'=>$where['product_name']]);
        }
        if(!empty($where['product_name_en'])) {
            $db->andWhere(['like','product_name_en'=>$where['product_name_en']]);
        }
        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    public function add($data)
    {
        $data['create_at'] = time();
        return yii::$app->db->createCommand()->insert($this->table, $data)->execute();
    }

    /**删除操作
     * @param $id
     * @return int
     */
    public function del($id)
    {
        return yii::$app->db->createCommand()->delete($this->table,['id'=>$id])->execute();
    }

    public function getQuoteById($id)
    {
        $db = new Query();
        $info = $db->select('*')->from($this->table)->where(['id' => $id])->limit(1)->one();
        return $info;
    }

    public function lockRecord($id,$status)
    {
        return yii::$app->db->createCommand()->update($this->table,['status'=>$status],['id'=>$id])->execute();
    }

    /**
     * @param $id
     * @return bool
     */
    public function checkEdit($id)
    {
        $db = new Query();
        $info = $db->select('status')->from($this->table)->where(['id'=>$id])->one();
        if ($info) {
            return $info['status']==2?false:true;
        }
    }
}